package lk.mrt.cse.wihidum.storage;

import org.apache.log4j.Logger;

import java.util.Collections.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * User: Hasini
 * Date: Mar 30, 2010
 * Time: 8:53:48 AM
 * To change this template use File | Settings | File Templates.
 */
public class BrokerList
{
        private static final Logger logger = Logger.getLogger (BrokerList.class);
        private static ArrayList<Broker> brokerList=new ArrayList<Broker>();
        public static ArrayList<Broker> shuffleAndReturnBrokerList()
        {
            Collections.shuffle(brokerList);
            logger.info("Top broker in admin service's list, after shuffling is"+brokerList.get(0).getAddress());
            ArrayList<Broker> subBrokerList=new ArrayList<Broker>();
            if((brokerList.size()>5)||(brokerList.size()==5))
            {
                for(int i=0;i<5;i++)
                {
                    Broker broker=brokerList.get(i);
                    subBrokerList.add(broker);
                    logger.info("The "+i+" broker in returned list, is"+broker.getAddress());
                }

            }
            else if(brokerList.size()<5)
            {
                for(int j=0;j<brokerList.size();j++)
                {
                    subBrokerList.add(brokerList.get(j));
                    logger.info("The returned broker is"+subBrokerList.get(j).getAddress());
                }

            }

            return subBrokerList;
        }
        public static boolean addBrokerToList(Broker broker)
        {
            boolean brokerAlreadyExist=false;
            if(brokerList.isEmpty())
            {
               brokerList.add(broker);
               brokerAlreadyExist=false;

            }
            else
            {
                for(Broker currentBroker:brokerList)
                {
                    String brokerAddress=currentBroker.getAddress();
                    if(broker.getAddress().equals(brokerAddress))
                    {
                        brokerAlreadyExist=true;
                        break;
                    }

                }
                if(!brokerAlreadyExist)
                {
                    brokerList.add(broker);
                    brokerAlreadyExist=false;
                }

            }
            return brokerAlreadyExist;

        }
        public static void removeBrokerFromList(String brokerAddress)
        {
          if(!isEmpty())
          {
              Iterator it=brokerList.iterator();
              int i=0;
               while(it.hasNext())
               {
                   i++;
                   Broker currentBroker=(Broker)it.next();
                   if(currentBroker.getAddress().equals(brokerAddress))
                   {
                       it.remove();
                       brokerList.remove(i-1);
                   }
               }
          }
        }
        public static boolean isEmpty()
        {
            boolean empty=false;
            empty=brokerList.isEmpty();            
            return empty;
        }

        public static ArrayList<Broker> getBrokerList()
        {
            return brokerList;
        }
}
